﻿using HPIT_Library_Modal;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.SessionState;
using System.Web.UI.WebControls;

namespace HPIT_Library
{
    /// <summary>
    /// LoginAjax 的摘要说明
    /// </summary>
    public class LoginAjax : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            var Acount = context.Request.Form["Acount"];
            LoginJson result = new LoginJson();
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            if (Acount == "login")
            {
                var name = context.Request.Form["MyName"];
                var pwd = context.Request.Form["MyPwd"];
                LoginInfo Info = HPIT_Library_BLL.LoginManger.Login(name, pwd);
                if (Info == null)
                {
                    //result.State = LoginJsonState.Error;
                    //result.Message = "账户或密码错误";
                    string json = serializer.Serialize(new LoginJson(LoginJsonState.Error,"账号或密码错误"));
                    context.Response.Write(json);
                }
                else
                {
                    context.Session["Info"] = Info;
                    //context.Response.Write("ok");
                    //result.State = LoginJsonState.Success;
                    //result.Message = "登录成功";
                    string json = serializer.Serialize(new LoginJson(LoginJsonState.Success,"登录成功"));
                    context.Response.Write(json);
                }
            }
            else if (Acount=="add")
            {
                var CategoryName = context.Request.Form["CategoryName"];
                if (HPIT_Library_BLL.categoryManger.Insert(CategoryName)>0)
                {
                    //result.State = LoginJsonState.Success;
                    //result.Message = "添加成功";
                    context.Response.Write(serializer.Serialize(new LoginJson(LoginJsonState.Success, "登录成功")));
                }
                else
                {
                    //result.State = LoginJsonState.Error;
                    //result.Message = "添加失败";
                    context.Response.Write(serializer.Serialize(new LoginJson(LoginJsonState.Error, "账号或密码错误")));
                }
;
            }
            else if(Acount=="delete")
            {
                var CategoryId =Convert.ToInt32( context.Request.Form["Pid"]);
                if (HPIT_Library_BLL.categoryManger.Delete(CategoryId)>0)
                {
                    result.State = LoginJsonState.Success;
                    result.Message = "删除成功!";
                    context.Response.Write(serializer.Serialize(result));
                }
                else
                {
                    //result.State = LoginJsonState.Error;
                    //result.Message = "删除失败!";
                    context.Response.Write(serializer.Serialize(new LoginJson(LoginJsonState.Error, "账号或密码错误")));
                }
            }
           

        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}